package org.neuedu.chapter10_0705.mydemo01;

import java.util.ArrayList;
import java.util.List;

public class Neuedu {
    // 学生名单
    private static final List<Student> STUD_LIST = new ArrayList<>();

    /**
     * 向名单中添加一名学生对象
     * @param s
     */
    public void addStudent(Student s) {
        for (Student student : STUD_LIST) {
            if (student.getId().equals(s.getId())) {
                System.out.println(s + " 学员添加失败，id不能相同");
                return;
            }
        }
        STUD_LIST.add(s);
        System.out.println("新增学员 "+s+" 成功");
    }

    /**
     * 根据id删除一名学生
     * @param id
     */
    public void delStudentById(Long id) {
        for (Student student : STUD_LIST) {
            if (student.getId().equals(id)) {
                STUD_LIST.remove(student);
                System.out.println("删除学员成功");
                return;
            }
        }
        System.out.println("删除失败");
    }

    /**
     * 根据 id 修改 学员信息
     * @param s
     */
    public void updateInfoById(Student s) {
        for (Student student : STUD_LIST) {
            if (student.getId().equals(s.getId())) {
                student.setName(s.getName());
                student.setAge(s.getAge());
                student.setGender(s.getGender());
                student.setStuno(s.getStuno());
                System.out.println("修改信息成功");
                return;
            }
        }
        System.out.println("修改失败");
    }

    /**
     * 根据 id 查询 学员信息
     * @param id
     * @return
     */
    public Student getInfoById(Long id) {
        for (Student student : STUD_LIST) {
            if (student.getId().equals(id)) {
                return student;
            }
        }
        return null;
    }

    /**
     * 遍历学员名单
     */
    public void showStuInfos() {
        for (Student student : STUD_LIST) {
            System.out.println(student);
        }
    }
}
